//
//  HZPeiziAddMoneyView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/12/11.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziAddMoneyView.h"
#import "HZLeftRightView.h"
#import "HZPeiziHeaderView.h"
#import "HZPeiziAddMoneyCell.h"

@interface HZPeiziAddMoneyView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)HZLeftRightView *pOne;
@property (nonatomic, strong)HZLeftRightView *pTwo;
@property (nonatomic, strong)HZLeftRightView *pThree;
@property (nonatomic, strong)HZLeftRightView *pFour;
@property (nonatomic, strong)HZLeftRightView *pFive;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;
@property (nonatomic, strong)HZPeiziHeaderView *pHeaderView;
@end

@implementation HZPeiziAddMoneyView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pOne = [HZLeftRightView new];
    _pTwo = [HZLeftRightView new];
    _pThree = [HZLeftRightView new];
    _pFour = [HZLeftRightView new];
    _pFive = [HZLeftRightView new];
    _pTableView = [UITableView new];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.tableFooterView = [UIView new];
    
    _pTableView.bounces = NO;
    _pTableView.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    [_pOne setLefttext:@"账户当前总资产" rigthtext:@"--"];
    [_pTwo setLefttext:@"初始金额" rigthtext:@"--"];
    [_pThree setLefttext:@"警戒线" rigthtext:@"--"];
    [_pFour setLefttext:@"可用于追加保证金余额" rigthtext:@"--"];
    [_pFive setLefttext:@"追加金额" rigthtext:@"--"];
    [_pFive showTextFiledWithPlaceholder:@"请输入要追加的金额"];
}
- (void)initConstraints
{
    [self addSubview:_pOne];
    [self addSubview:_pTwo];
    [self addSubview:_pThree];
    [self addSubview:_pFour];
    [self addSubview:_pFive];
    [self addSubview:_pTableView];
    
    [_pOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(_pTwo.mas_top).offset(-1);
    }];
    [_pTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_pOne);
        make.bottom.mas_equalTo(_pThree.mas_top).offset(-1);
    }];
    [_pThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_pOne);
        make.bottom.mas_equalTo(_pFour.mas_top).offset(-1);
    }];
    [_pFour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_pOne);
        make.bottom.mas_equalTo(_pFive.mas_top).offset(-1);
    }];
    [_pFive mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_pOne);
        make.height.mas_equalTo(50);
        make.height.mas_equalTo(@[_pOne, _pTwo, _pThree, _pFour]);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pFive.mas_bottom).offset(20);
        make.left.right.bottom.mas_equalTo(self);
    }];
}

- (void)p_updateNewData:(NSArray *)data
{
    if (data && data.count > 0) {
        _pDataList = data;
        [_pTableView reloadData];
    }
}
- (void)updateMoneyData:(HZPeiziAccountMoneyModel *)data
{
    [_pOne setRighttext:[NSString stringWithFormat:@"%0.2f", [data.total_assert floatValue]]];
    [_pTwo setRighttext:data.p_init_balance];
    [_pThree setRighttext:data.fml];
}
- (void)updateEableMoney:(NSString *)money
{
    [_pFour setRighttext:money];
}
- (NSString *)getOutMoney
{
    return [_pFive getTextFieldText];
}
- (void)clearMoney
{
    [_pFive setTextfieldText:@""];
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell *cell = nil;
    HZPeiziMoneyOutListModel *model = _pDataList[indexPath.row];
    cell = [HZPeiziAddMoneyCell cellTableView:tableView indexPath:indexPath cellModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return self.pHeaderView;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return 30;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (HZPeiziHeaderView *)pHeaderView {
    if (!_pHeaderView) {
        _pHeaderView = [HZPeiziHeaderView new];
        [_pHeaderView setTexts:@[@"申请日期",@"申请时间",@"追加金额",@"申请进度"]];
    }
    return _pHeaderView;
}

@end
